package com.zzy.demo.common.util.export;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import com.alibaba.excel.util.ListUtils;
import com.zzy.cms.core.util.string.StringsUtil;
import com.zzy.demo.common.dto.export.ColHeaderVO;

/**
 * 
 * @author guokaige
 *
 */
public class TopEasyExcelUtil {



	public static List<List<String>> buildHead(List<ColHeaderVO> colList) {
	    List<List<String>> list = ListUtils.newArrayList();
	    
	    for(ColHeaderVO colHeaderVO : colList) {
	    	String columnName = colHeaderVO.getColumnName();
	    	//初始化长度
	    	TopEasyExcelUtil.initColWidth(colHeaderVO, columnName);
	    	
	    	List<String> headTemp = ListUtils.newArrayList();
	    	headTemp.add(columnName);
	    	list.add(headTemp);
	    }
	    return list;
	}
	
	public static List<List<Object>> buildDataList() {
	    List<List<Object>> list = ListUtils.newArrayList();
	    for (int i = 0; i < 10; i++) {
	        List<Object> data = ListUtils.newArrayList();
	        data.add("字符串" + i);
	        data.add(0.56);
	        data.add(new Date());
	        list.add(data);
	    }
	    return list;
	}
	
	
	/**
	 * 初始化列宽
	 * @param headerVO
	 * @param value
	 */
	public static void initColWidth(ColHeaderVO headerVO, String value) {
		if (StringsUtil.hasText(value)) {
			int width = 70;
			try {
				int length = value.getBytes("GBK").length;
				int upperCount = SummaryReportStypleUtil.countUpperCaseLetters(value);
				length += upperCount * 0.5;
				width = 7 * length + 46;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if (width > SummaryReportStypleUtil.MAX_COL_WIDTH) {
				width = SummaryReportStypleUtil.MAX_COL_WIDTH;
			}
			if (width > headerVO.getMinWidth()) {
				headerVO.setMinWidth(width);
			}
		}
	}
	

	public static void main(String[] args) throws IOException, EncryptedDocumentException, InvalidFormatException {

	}

}